package com.lck.main.server;

import com.lck.main.model.MyMessage;
import com.lck.main.model.User;
import com.lck.main.util.SocketUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

public class ChessServer {

    private static Map <String,ServerThread> clients;//保存登录的所有的客户端

    public static void main(String[] args) {
        new ChessServer().start();
    }
    public void start(){
        try {
            ServerSocket server = new ServerSocket(8080);
            clients = new HashMap<>();
            System.out.println("服务端启动成功");
            while(true){
                Socket accept = server.accept();
                ServerThread st = new ServerThread(accept);
                st.start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class ServerThread extends Thread {
        private Socket socket;

        ServerThread(Socket socket) {
            this.socket = socket;
        }
        public Socket getSocket() {
            return socket;
        }
        @Override
        public void run() {
            try {
                while (true){
                    Object receive = SocketUtil.getInstance().receive(socket);
                    System.out.println(receive);
                    if (receive instanceof MyMessage) {
                        MyMessage request = (MyMessage) receive;
                        switch(request.getType()){
                            case LOGIN:
                                login(request);
                                break;
                            case REG:
                                reg(request);
                                break;
                            case FORGET:
                                forget(request);
                                break;
                            case LIST:
                                list(request);
                                break;
                            case FIGHT:
                                fight(request);
                                break;
                            case MOVE:
                                move(request);
                                break;
                            case EAT:
                                eat(request);
                                break;
                            case PEACE:
                                qiuhe(request);
                                break;
                            case SURRENDER:
                                break;
                        }
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                SocketUtil.getInstance().close(null, null, socket);
            }

        }
        private Vector getAccountList() {
            Vector<String> list = new Vector<>();
            Set<String> keySet = clients.keySet();
            Iterator<String> iterator = keySet.iterator();
            while(iterator.hasNext()){
                list.add(iterator.next());
            }
            return list.size() == 0?null:list;
        }

        private MyMessage c2c(MyMessage.Type type, MyMessage req) {
            String to = req.getTo();
            ServerThread stTo = clients.get(to);
            MyMessage resp = new MyMessage();
            resp.setFrom(req.getFrom());
            resp.setTo(req.getTo());
            resp.setContent(req.getContent());
            resp.setType(type);
            SocketUtil.getInstance().send(stTo.getSocket(), resp);
            return resp;
        }

        private void qiuhe(MyMessage req){
            System.out.println("qiuhe");
            c2c(MyMessage.Type.PEACE, req);
        }

        private void fight (MyMessage req){
//            String from = req.getFrom();
//            String to = req.getTo();
//            ServerThread stFrom = clients.get(from);
//            ServerThread stTo = clients.get(to);
//            MyMessage resp = new MyMessage();
//            //规定挑战发起者拿到红棋
//            resp.setFromPlayer(0);
//            resp.setToPlayer(1);
//            resp.setFrom(req.getFrom());
//            resp.setTo(req.getTo());
//            resp.setType(MyMessage.Type.FIGHT_SUCCESS);
//            SocketUtil.send(stFrom.getSocket(),resp);
//            SocketUtil.send(stTo.getSocket(),resp);
            System.out.println("fight");
            MyMessage resp = c2c(MyMessage.Type.FIGHT_SUCCESS, req);
            SocketUtil.getInstance().send(socket, resp);
        }

        private void list (MyMessage req){
            MyMessage resq = new MyMessage();
            resq.setType(MyMessage.Type.SUCCESS);
            resq.setContent(getAccountList());
            clients.forEach((k,v)->{
                SocketUtil.getInstance().send(v.getSocket(),resq);
            });
        }

        private void login (MyMessage msg){
            //假设登陆成功
            Object content = msg.getContent();
            //将数据保存到客户端map中
            clients.put(((User)content).getAccount(),this);
            //发送消息回去
            MyMessage response = new MyMessage();
            response.setType(MyMessage.Type.SUCCESS);
            SocketUtil.getInstance().send(socket,response);
        }
        private void reg (MyMessage req){

        }
        private void forget (MyMessage req){

        }
        private void move (MyMessage req){
            System.out.println("move");
            c2c(MyMessage.Type.MOVE, req);
        }
        private void eat (MyMessage req){
            System.out.println("eat");
            c2c(MyMessage.Type.EAT, req);
        }
    }

}
